// DlgComplete.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgComplete.h"
#include "DlgPreviewHTML.h"
#include "SendMessage.h"


// CDlgComplete dialog

IMPLEMENT_DYNAMIC(CDlgComplete, CDialog)

CDlgComplete::CDlgComplete(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgComplete::IDD, pParent)
	, m_szInfo(_T(""))
	, m_szContact(_T(""))
	, m_szInstructions(_T(""))
	, m_bElectronic(FALSE)
	, m_dServiceFee(0)
	, m_dCopyrightFee(0)
	, m_dMiscFee(0)
{
	m_nCollectionID = 0;
	m_pszaCollection = NULL;
	m_bUsePrintTemplate = FALSE;
}

CDlgComplete::~CDlgComplete()
{
}

void CDlgComplete::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_STATIC_INFO, m_szInfo);
	DDX_Text(pDX, IDC_EDIT_REQUESTER, m_szContact);
	DDX_Text(pDX, IDC_EDIT_INSTRUCTIONS, m_szInstructions);
	DDX_Control(pDX, IDC_COMBO_COLLECTION, m_ctrlCollection);
	DDX_Check(pDX, IDC_CHECK_ELECTRONIC, m_bElectronic);
	DDX_Text(pDX, IDC_EDIT_SERVICE_FEE, m_dServiceFee);
	DDX_Text(pDX, IDC_EDIT_COPYRIGHT_FEE, m_dCopyrightFee);
	DDX_Text(pDX, IDC_EDIT_MISC_FEE, m_dMiscFee);
}


BEGIN_MESSAGE_MAP(CDlgComplete, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_PRINT_REQUEST, &CDlgComplete::OnBnClickedButtonPrintRequest)
	ON_BN_CLICKED(IDC_BUTTON_CONTACTED, &CDlgComplete::OnBnClickedButtonContacted)
	ON_BN_CLICKED(IDC_BUTTON_EMAIL, &CDlgComplete::OnBnClickedButtonEmail)
	ON_CBN_SELCHANGE(IDC_COMBO_COLLECTION, &CDlgComplete::OnCbnSelchangeComboCollection)
END_MESSAGE_MAP()


// CDlgComplete message handlers

BOOL CDlgComplete::OnInitDialog()
{
	CDialog::OnInitDialog();

	// set title
	CString szText;
	szText.Format( _T("Completing Request: %.8ld"), m_nItemID );
	SetWindowText( szText );

	// get request
	REQUEST_SET Request;
	if( !appConfig.m_SQL.GetRequestSet( &Request, m_nItemID ))
	{
		AfxMessageBox( _T("ERROR getting request.") );
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// get variables
	m_szRequest = Request.m_request;
	m_szDescription = Request.m_request.Left( DETAIL_LENGTH );
	m_szInstructions = Request.m_instructions;
	m_szDateIn = Request.m_date_in.Format( appConfig.m_General.szDateFormat );
	m_nUserID = Request.m_user_id;
	m_nCollectionID = Request.m_collection_id;
	m_dServiceFee = Request.m_fee_service;
	m_dCopyrightFee = Request.m_fee_copyright;
	m_dMiscFee = Request.m_fee_misc;

	INT nCurSel = 0;
	INT nSel = 0;
	CString szTemp;

	ASSERT( m_pszaCollection != NULL );

	// get user
	USER_SET User;
	if( !appConfig.m_SQL.GetUserSet( &User, m_nUserID ))
	{
		AfxMessageBox( _T("ERROR getting user.") );
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// get variables
	m_szTo = User.m_email;
	// set info
	m_szInfo.Format( _T("Name: %s\r\nCompany: %s\r\nDate In: %s"), User.m_name, User.m_company, m_szDateIn );
	// set contact info
	m_szContact.Format( _T("Name: %s\r\nCompany: %s\r\nPhone: %s\r\nFax: %s\r\nAddress:\r\n%s\r\n%s\r\n%s\r\n%s, %s\r\n%s"),
		User.m_name, User.m_company, User.m_phone, User.m_fax, User.m_address1, User.m_address2,
		User.m_city, User.m_state, User.m_zipcode, User.m_country );

	// get library info
	LIBRARY_LOOKUP Library;
	appConfig.m_SQL.GetDescriptionLibrary( &Library, Request.m_library_id );
	// get variables
	m_szFrom = Library.szEmail;
	m_szName = Library.szName;
	m_szSubject = Library.szSubject;

	// check for electronic
	COLLECTION_LOOKUP Collection;
	appConfig.m_SQL.GetDescriptionCollection( &Collection, Request.m_collection_id );
	// set electronc
	m_bElectronic = Collection.nElectronic;

	// check for valid staff
	if( m_nStaffID > 0 )
	{
		// get staff
		STAFF_SET Staff;
		appConfig.m_SQL.GetStaffSet( &Staff, m_nStaffID );
		// set template
		m_bUsePrintTemplate = Staff.m_use_print_template;
		// override with default
		if( Staff.m_default_collection >= 0 )		
			m_nCollectionID = Staff.m_default_collection;
	}

	// set m_ctrlCollection
	for( INT nIndex = 0; nIndex < m_pszaCollection->GetCount(); nIndex++ )
	{
		szTemp = m_pszaCollection->GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlCollection.AddString( szTemp );
			m_ctrlCollection.SetItemData( nSel, nIndex );
			if( Request.m_collection_id == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlCollection.SetCurSel( nCurSel );

	// show finalcials
	if( appConfig.m_Options.bDisableFinacial )
	{
		GetDlgItem( IDC_STATIC_SERVICE_FEE )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_SERVICE_FEE )->ShowWindow( SW_HIDE );

		GetDlgItem( IDC_STATIC_MISC_FEE )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_MISC_FEE )->ShowWindow( SW_HIDE );

		GetDlgItem( IDC_STATIC_COPYRIGHT_FEE )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_COPYRIGHT_FEE )->ShowWindow( SW_HIDE );
	}

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgComplete::OnOK()
{
	// update data
	UpdateData();

	// get request
	REQUEST_SET Request;
	if( !appConfig.m_SQL.GetRequestSet( &Request, m_nItemID ))
	{
		AfxMessageBox( _T("ERROR getting request.") );
		EndDialog( IDCANCEL );
		return;
	}
	// set variables
	Request.m_status_id = STATUS_ID_COMPLETE;
	Request.m_staff_id = m_nStaffID;
	Request.m_library_id = m_nLibraryID;
	Request.m_date_out = COleDateTime::GetCurrentTime();
	Request.m_touched = TRUE;
	Request.m_material = m_bElectronic;
	Request.m_fee_service = m_dServiceFee;
	Request.m_fee_copyright = m_dCopyrightFee;
	Request.m_fee_misc = m_dMiscFee;

	// get collection id
	m_nCollectionID = m_ctrlCollection.GetItemData( m_ctrlCollection.GetCurSel() );
	if( m_nCollectionID < 0 )
		return;
	Request.m_collection_id = m_nCollectionID;
	// update request
	appConfig.m_SQL.SetRequestSet( &Request );


	CDialog::OnOK();
}

void CDlgComplete::OnBnClickedButtonPrintRequest()
{
	// update data
	UpdateData();

	// get request
	REQUEST_SET Request;
	if( !appConfig.m_SQL.GetRequestSet( &Request, m_nItemID ))
	{
		AfxMessageBox( _T("ERROR getting request.") );
		EndDialog( IDCANCEL );
		return;
	}

	// check for redirected
	if( Request.m_redirected > 0 )
	{
		CString szTemp;
		szTemp.Format( _T("ATTENTION: This request has been redirected %d times.\n\nIs request ready for pickup or been mailed?"), Request.m_redirected );
		if( AfxMessageBox( szTemp, MB_YESNO ) == IDNO )
			return;
	}

	// check for print template
	if( !appConfig.m_Printing.szRequestTemplate.IsEmpty() && m_bUsePrintTemplate )
	{
		CString szHTMLRequest;
		// read templates
		szHTMLRequest = appConfig.ReadFile( appConfig.m_Printing.szRequestTemplate );
		// format macros
		appConfig.FormatMacros( &szHTMLRequest, m_nItemID );
		// print requests
		CDlgPreviewHTML dlgPreview;
		dlgPreview.m_bPrint = TRUE;
		dlgPreview.m_bPromptPrinter = TRUE;
		dlgPreview.m_bPrintPreview = appConfig.m_Printing.bPrintPreview;
		dlgPreview.m_szHTML = szHTMLRequest;
		// show message
		dlgPreview.DoModal();
	}
	else
	{
		CDC dc;
		DOCINFO di;
		CPrintInfo Info;
		CPrintDialog printDlg(FALSE);
		
		// get printer
		if( printDlg.DoModal() == IDCANCEL )
			 return;

		// get CDC	
		HDC hdc = printDlg.CreatePrinterDC();
		if( !dc.Attach( hdc ))
		{
			AfxMessageBox(_T("No printer found!"));
			return;
		}

		// set printing
		dc.m_bPrinting = TRUE;		
		// Initialise
		::ZeroMemory (&di, sizeof (DOCINFO));
		di.cbSize = sizeof (DOCINFO);
		di.lpszDocName = _T("Celsus");
		// Begin a new print job
		dc.StartDoc(&di);
		// Get the printing extents and store in the m_rectDraw field of a CPrintInfo object		
		INT maxw = dc.GetDeviceCaps(HORZRES);
		INT maxh = dc.GetDeviceCaps(VERTRES);
		Info.m_rectDraw.SetRect(0, 0, maxw, maxh);
		// print request
		if( appConfig.PrintRequest( &dc, m_nItemID, 0 ))
		{
			// user contacted enable ok
			GetDlgItem( IDOK )->EnableWindow();
		}
		// end page
		dc.EndPage();
		// end a print job
		dc.EndDoc();
	}
}

void CDlgComplete::OnBnClickedButtonContacted()
{
	// update data
	UpdateData();

	// get request
	REQUEST_SET Request;
	if( !appConfig.m_SQL.GetRequestSet( &Request, m_nItemID ))
	{
		AfxMessageBox( _T("ERROR getting request.") );
		EndDialog( IDCANCEL );
		return;
	}

	// check for redirected
	if( Request.m_redirected > 0 )
	{
		CString szTemp;
		szTemp.Format( _T("ATTENTION: This request has been redirected %d times.\n\nIs request ready for pickup or been mailed?"), Request.m_redirected );
		if( AfxMessageBox( szTemp, MB_YESNO ) == IDNO )
			return;
	}

	// user contacted enable ok
	GetDlgItem( IDOK )->EnableWindow();
}

void CDlgComplete::OnBnClickedButtonEmail()
{
	// update data
	UpdateData();

	// get request
	REQUEST_SET Request;
	if( !appConfig.m_SQL.GetRequestSet( &Request, m_nItemID ))
	{
		AfxMessageBox( _T("ERROR getting request.") );
		EndDialog( IDCANCEL );
		return;
	}

	// check for redirected
	if( Request.m_redirected > 0 )
	{
		CString szTemp;
		szTemp.Format( _T("ATTENTION: This request has been redirected %d times.\n\nIs request ready for pickup or been mailed?"), Request.m_redirected );
		if( AfxMessageBox( szTemp, MB_YESNO ) == IDNO )
			return;
	}

	// get collection id
	m_nCollectionID = m_ctrlCollection.GetItemData( m_ctrlCollection.GetCurSel() );
	if( m_nCollectionID < 0 )
		return;

	//description_method
	METHOD_LOOKUP Method;
	appConfig.m_SQL.GetDescriptionMethod( &Method, Request.m_method_id );

	// set message
	if( !Method.szMessageFile.IsEmpty() )
		m_szMessage = appConfig.ReadFile( Method.szMessageFile );
	else
		m_szMessage = appConfig.ReadFile( appConfig.m_Email.szNonEDDMessageFile );

	// set subject
	if( !Method.szMessageSubject.IsEmpty() )
		m_szSubject = Method.szMessageSubject;

	// format message
	appConfig.FormatMacros( &m_szMessage, m_nItemID );
	// format subject macros
	appConfig.FormatMacros( &m_szSubject, m_nItemID, -1, STATUS_ID_COMPLETE );

	// preview email
	CDlgPreviewHTML dlgPreview;
	dlgPreview.m_bSend = TRUE;
	dlgPreview.m_szHTML = m_szMessage;
	dlgPreview.m_szSubject = m_szSubject;

	// show message
	if( dlgPreview.DoModal() != IDOK )
		return;

	// get new message
	m_szMessage = dlgPreview.m_szHTML;
	m_szSubject = dlgPreview.m_szSubject;

	// set variables
	Request.m_status_id = STATUS_ID_COMPLETE;
	Request.m_staff_id = m_nStaffID;
	Request.m_library_id = m_nLibraryID;
	Request.m_date_out = COleDateTime::GetCurrentTime();
	Request.m_touched = TRUE;
	Request.m_material = m_bElectronic;
	Request.m_collection_id = m_nCollectionID;
	Request.m_fee_service = m_dServiceFee;
	Request.m_fee_copyright = m_dCopyrightFee;
	Request.m_fee_misc = m_dMiscFee;
	// update request
	appConfig.m_SQL.SetRequestSet( &Request );

	// send message
	CSendMessage send;
	if( !send.SendEmail( m_szName, m_szFrom, m_szTo, m_szSubject, m_szMessage ))
		return;

	// close dialog
	CDialog::OnOK();
}

void CDlgComplete::OnCbnSelchangeComboCollection()
{
	// get collection id
	m_nCollectionID = m_ctrlCollection.GetItemData( m_ctrlCollection.GetCurSel() );

	// check for electronic
	COLLECTION_LOOKUP Collection;
	appConfig.m_SQL.GetDescriptionCollection( &Collection, m_nCollectionID );
	// set electronc
	m_bElectronic = Collection.nElectronic;

	// update data
	UpdateData( FALSE );
}
